import cv2
import matplotlib.pyplot as plt
import numpy as np


def show(img):
    cv2.imshow("img", img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


img = cv2.imread("01_Picture/01_cat.jpg", cv2.IMREAD_GRAYSCALE)
hist = cv2.calcHist([img], [0], None, [256], [0, 256])
print(hist.shape)
print((img.ravel()).shape)
# img.ravel()将 img 拉成一维数组
plt.hist(img.ravel(),256)
plt.show()

img = cv2.imread('01_Picture/01_Cat.jpg')
color = ('b','g','r')
for i,col in enumerate(color):
    histr = cv2.calcHist([img],[i],None,[256],[0,256])
    print(histr.shape)
    plt.plot(histr,color=col)
    plt.xlim([0,256])


img = cv2.imread('01_Picture/01_Cat.jpg',0)
show(img)
print(img.shape[:2])
mask = np.zeros(img.shape[:2],np.uint8)
print(mask.shape)
mask[100:300,100:400] = 255
show(mask)
masked_img = cv2.bitwise_and(img,img,mask=mask) # 与操作
show(masked_img)






